home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_09_12
/
9n12117a
< prev
next >
Wrap
Text File
|
1991-10-19
|
2KB
|
70 lines
/* LISTING 8 - OBJ.C */
/* OBJ.C - implement private data storage
* for each object */
/* note we no longer need to include obj.h */
#include <stdio.h>
#define GETCOLOR 0
#define SETCOLOR 1
#define YELLOW 14
#define BLUE 9
#define RED 12
/* duplication of typedef struct circle */
typedef struct circle
{
void *pprivate;
/* action pack is now accessed with
* a pointer to a pointer to a function
* returning int
*/
int (**pcact)();
} CIRCLE;
main()
{
CIRCLE c1, c2; /* declare two circles */
int color;
/* call the constructor for each circle */
constructor(&c1, YELLOW);
constructor(&c2, RED);
/* use the act pack to get color */
color = (*c1.pcact[GETCOLOR])(&c1);
printf("Color of c1 is %d\n", color);
color = (*c2.pcact[GETCOLOR])(&c2);
printf("Color of c2 is %d\n", color);
(*c1.pcact[SETCOLOR])(&c1, BLUE);
printf("Setting color of c1 to BLUE\n");
color = (*c1.pcact[GETCOLOR])(&c1);
printf("Color of c1 is now %d\n", color);
(*c2.pcact[SETCOLOR])(&c2, YELLOW);
printf("Setting color of c2 to YELLOW\n");
color = (*c2.pcact[GETCOLOR])(&c2);
printf("Color of c2 is now %d\n", color);
destructor(&c1); /* free circle storage */
destructor(&c2);
}
/* SAMPLE OUTPUT FROM LISTING 9 */
Color of c1 is 14
Color of c2 is 12
Setting color of c1 to BLUE
Color of c1 is now 9
Setting color of c2 to YELLOW
Color of c2 is 14
*/